{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": true,
        "id": "cmjOVVtJdiPZ"
      },
      "outputs": [],
      "source": [
        "!git clone https://github.com/cg123/mergekit.git\n",
        "%cd mergekit\n",
        "%pip install -e ."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "id": "84cRJT6_ecbw"
      },
      "outputs": [],
      "source": [
        "OUTPUT_PATH = \"./merged\"  # folder to store the result in\n",
        "LORA_MERGE_CACHE = \"/tmp\"  # change if you want to keep these for some reason\n",
        "CONFIG_YML = \"./examples/gradient-slerp.yml\"  # merge configuration file\n",
        "COPY_TOKENIZER = True  # you want a tokenizer? yeah, that's what i thought\n",
        "LAZY_UNPICKLE = False  # experimental low-memory model loader\n",
        "LOW_CPU_MEMORY = False  # enable if you somehow have more VRAM than RAM+swap"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "6nw26xQLkBax"
      },
      "outputs": [],
      "source": [
        "# actually do merge\n",
        "import torch\n",
        "import yaml\n",
        "\n",
        "from mergekit.config import MergeConfiguration\n",
        "from mergekit.merge import MergeOptions, run_merge\n",
        "\n",
        "with open(CONFIG_YML, \"r\", encoding=\"utf-8\") as fp:\n",
        "    merge_config = MergeConfiguration.model_validate(yaml.safe_load(fp))\n",
        "\n",
        "run_merge(\n",
        "    merge_config,\n",
        "    out_path=OUTPUT_PATH,\n",
        "    options=MergeOptions(\n",
        "        lora_merge_cache=LORA_MERGE_CACHE,\n",
        "        cuda=torch.cuda.is_available(),\n",
        "        copy_tokenizer=COPY_TOKENIZER,\n",
        "        lazy_unpickle=LAZY_UNPICKLE,\n",
        "        low_cpu_memory=LOW_CPU_MEMORY,\n",
        "    ),\n",
        ")\n",
        "print(\"Done!\")"
      ]
    }
  ],
  "metadata": {
    "accelerator": "GPU",
    "colab": {
      "gpuType": "T4",
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.10.13"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
